package com.linkvin.ec.hcs.service.security.dao;

import com.google.common.base.Strings;

import com.linkvin.ec.hcs.dao.mapper.sysadmin.AccountUserMapper;
import com.linkvin.ec.hcs.dao.model.sysadmin.bo.AccountUserBo;
import com.linkvin.ec.hcs.service.security.domains.JwtUser;
import com.linkvin.ec.hcs.service.security.factory.JwtUserFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * Created by qichao on 2017/11/29.
 */
@Service
public class JwtUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private AccountUserMapper accountUserMapper;

    @Override
    public UserDetails loadUserByUsername(String code) throws UsernameNotFoundException {
        if (Strings.isNullOrEmpty(code)) {
            throw new IllegalArgumentException("accountCode must not bu null");
        }

        //List<AccountUserDTO> users = accountUserMapper.findAccountByCode(accountCode);
        AccountUserBo acUser = accountUserMapper.selectAcUserByUserKey(code);
        if(Objects.equals(acUser,null)){
            throw new UsernameNotFoundException("账号不存在");
        }else {
            JwtUser jwtUser = JwtUserFactory.create(acUser);
            return jwtUser;
        }
    }
}
